package com.bs.teachassistant.fragment;

import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bs.teachassistant.R;
import com.bs.teachassistant.activity.AddStudActivity;
import com.bs.teachassistant.adapter.CommDataAdapter;
import com.bs.teachassistant.common.Comm;
import com.bs.teachassistant.entity.StudBean;
import com.bs.teachassistant.service.ResourceAssetsHander;
import com.bs.teachassistant.service.WebConfig;
import com.bs.teachassistant.service.WebHttpServer;
import com.bs.teachassistant.utils.ExcelUtil;
import com.bs.teachassistant.utils.FormatUtils;
import com.bs.teachassistant.utils.GsonUtils;
import com.bs.teachassistant.utils.LogUtil;
import com.hjq.toast.ToastUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.bs.teachassistant.utils.FormatUtils.getSDKVersionNumber;

/**
 * Created by limh on 2020/4/24.
 * 签到模块
 */
@ContentView(R.layout.fragment_sign)
public class SignFragment extends BaseFragment {
    @ViewInject(R.id.line_sign)
    LinearLayout lineSign;
    @ViewInject(R.id.line_sign_status)
    LinearLayout lineStatus;
    @ViewInject(R.id.image_check)
    ImageView imageCheck;
    @ViewInject(R.id.txt_ip)
    TextView txtIp;
    @ViewInject(R.id.list_sign)
    ListView listSign;

    //签到服务器
    private WebHttpServer mHttpServer;
    private UpdateSignStudBroadcast updateSignStudBroadcast;
    private ArrayList<StudBean> studDatas = new ArrayList<>();
    private CommDataAdapter<StudBean> adapter;

    private String TAG = "SignFragment";

    @Override
    public void initViews() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Comm.ACTION_UPDATE_ADD_SIGN);
        updateSignStudBroadcast = new UpdateSignStudBroadcast();
        LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(updateSignStudBroadcast, filter);
    }

    @Override
    public void initDatas() {
        LogUtil.d(TAG, "刷新界面");
        if (studDatas.size() > 0) {
            lineStatus.setVisibility(View.VISIBLE);
        } else {
            lineStatus.setVisibility(View.INVISIBLE);
        }
        if (null == adapter) {
            adapter = new CommDataAdapter<StudBean>(studDatas, R.layout.view_item_sign) {
                @Override
                public void bindView(ViewHolder holder, StudBean obj) {
                    holder.setText(R.id.txt_id, "" + (studDatas.indexOf(obj) + 1));
                    holder.setText(R.id.txt_sign_id, obj.getStuId());
                    holder.setText(R.id.txt_sign_name, obj.getName());
                    holder.setText(R.id.txt_sign_status, obj.getStatus());
                    holder.setText(R.id.txt_sign_time, obj.getSignTime());
                    if (obj.getStatus().equals("请假")) {
                        holder.setTextColor(R.id.txt_sign_status, Color.RED);
                    } else {
                        holder.setTextColor(R.id.txt_sign_status, Color.GRAY);
                    }
                }
            };
            listSign.setAdapter(adapter);
        } else {
            adapter.setmData(studDatas);
        }
    }

    @Event(value = {R.id.btn_copy, R.id.btn_finish, R.id.txt_start, R.id.txt_export,
            R.id.btn_resign, R.id.txt_add_info, R.id.image_check}, type = View.OnClickListener.class)
    private void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_copy:
                if (getSDKVersionNumber() >= 11) {
                    android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboardManager.setText(txtIp.getText().toString());
                } else {
                    // 得到剪贴板管理器
                    android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboardManager.setPrimaryClip(ClipData.newPlainText(null, txtIp.getText().toString()));
                }
                Toast.makeText(getActivity(), "签到地址已复制", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_finish:
                lineSign.setVisibility(View.VISIBLE);
                mHttpServer.stopServer();
                break;
            case R.id.txt_start:
                if (TextUtils.isEmpty(userPreference.getString("students", ""))) {
                    Toast.makeText(getActivity(), "请先添加学生信息", Toast.LENGTH_SHORT).show();
                    break;
                }
                if (null != mHttpServer && mHttpServer.isStart()) {
                    Toast.makeText(getActivity(), "已经在签到~", Toast.LENGTH_SHORT).show();
                    return;
                }
                lineSign.setVisibility(View.GONE);
                txtIp.setText(String.format(Locale.CHINA, "http://%s:%d", getIp(), 8088));
                //启动手机端签到服务器
                WebConfig config = new WebConfig();
                config.setPort(8088);
                //最大访问人数
                config.setMaxParallels(100);
                mHttpServer = new WebHttpServer(config);
                mHttpServer.registerResourceHander(new ResourceAssetsHander(getActivity()));
                mHttpServer.startServer();
                break;
            case R.id.btn_resign:
                userPreference.edit().putString("allStud", "").apply();
                adapter.clear();
                Toast.makeText(getActivity(), "重置成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.txt_add_info:
                Intent intent = new Intent();
                intent.putExtra("type", "add");
                intent.setClass(getActivity(), AddStudActivity.class);
                studDatas.clear();
                adapter.clear();
                startActivity(intent);
                break;
            case R.id.image_check:
                Intent intent1 = new Intent();
                intent1.putExtra("type", "check");
                intent1.setClass(getActivity(), AddStudActivity.class);
                startActivity(intent1);
                break;
            case R.id.txt_export:
                //权限检查
                AndPermission.with(this)
                        .runtime()
                        .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
                        .onGranted(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                exportExcel();
                            }
                        }).onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        ToastUtils.show("请先打开存储权限，否则无法导出签到记录");
                    }
                }).start();
                break;
            default:
                break;
        }
    }

    /**
     * 导出文件
     */
    private void exportExcel() {
        String fileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getAbsoluteFile().getPath() + "/签到记录_" + FormatUtils.getFileSuffixTime() + ".xls";
        String[] title = {"序号", "学号", "姓名", "签到状态", "签到时间","签到IP地址"};
        ExcelUtil.initExcel(fileName, title);
        List<StudBean> signStuds = GsonUtils.StudGsonToBean(userPreference.getString("signs", ""));
        Map<String, String> signMap = new HashMap<>();
        for (StudBean item : signStuds) {
            signMap.put(item.getStuId(), item.getStatus() + "_" + item.getSignTime()+"_"+item.getSignIp());
        }
        List<ArrayList<String>> excelList = new ArrayList<>();
        List<StudBean> allStuds = GsonUtils.StudGsonToBean(userPreference.getString("students", ""));
        for (int i = 0; i < allStuds.size(); i++) {
            StudBean bean = allStuds.get(i);
            ArrayList<String> row = new ArrayList<>();
            row.add("" + (i + 1));
            row.add(bean.getStuId());
            row.add(bean.getName());
            if (signMap.containsKey(bean.getStuId())) {
                row.add(signMap.get(bean.getStuId()).split("_")[0]);
                row.add(signMap.get(bean.getStuId()).split("_")[1]);
                row.add(signMap.get(bean.getStuId()).split("_")[2]);
            } else {
                row.add("旷课");
                row.add("--");
                row.add("--");
            }
            excelList.add(row);
        }
        ExcelUtil.writeObjListToExcel(excelList, fileName, this.getActivity());
    }

    /**
     * @return 本机IP地址
     */
    private String getIp() {
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (null != wifiManager && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        if (null != wifiManager) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            return intToIp(ipAddress);
        } else {
            return "0.0.0.0";
        }
    }

    private String intToIp(int i) {

        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

    /**
     * 签到广播监听
     */
    private class UpdateSignStudBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (null == intent || null == intent.getAction()) {
                return;
            }
            if (Comm.ACTION_UPDATE_ADD_SIGN.equals(intent.getAction())) {
                imageCheck.setVisibility(View.VISIBLE);
                StudBean stud = intent.getParcelableExtra("stud");
                studDatas.add(stud);
                userPreference.edit().putString("signs", GsonUtils.GsonString(studDatas)).apply();
                LogUtil.w(TAG, "stud" + stud.getStuId() + "  " + stud.getStatus());
                initDatas();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != updateSignStudBroadcast) {
            LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(updateSignStudBroadcast);
        }
    }
}
